using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace Commerce.Common
{
	/// <summary>
	/// Strongly-typed collection for the Coupon class.
	/// </summary>
	[Serializable]
	public partial class CouponCollection : ActiveList<Coupon,CouponCollection> 
	{	   
		public CouponCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the CSK_Coupons table.
	/// </summary>
	[Serializable]
	public partial class Coupon : ActiveRecord<Coupon>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public Coupon()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Coupon(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

        
		public Coupon(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Coupon(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }

		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("CSK_Coupons", TableType.Table, DataService.GetInstance("dashCommerce"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarCouponCode = new TableSchema.TableColumn(schema);
				colvarCouponCode.ColumnName = "CouponCode";
				colvarCouponCode.DataType = DbType.String;
				colvarCouponCode.MaxLength = 30;
				colvarCouponCode.AutoIncrement = false;
				colvarCouponCode.IsNullable = false;
				colvarCouponCode.IsPrimaryKey = true;
				colvarCouponCode.IsForeignKey = false;
				colvarCouponCode.IsReadOnly = false;
				colvarCouponCode.DefaultSetting = @"";
				colvarCouponCode.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCouponCode);
				
				TableSchema.TableColumn colvarCouponTypeId = new TableSchema.TableColumn(schema);
				colvarCouponTypeId.ColumnName = "CouponTypeId";
				colvarCouponTypeId.DataType = DbType.Int32;
				colvarCouponTypeId.MaxLength = 0;
				colvarCouponTypeId.AutoIncrement = false;
				colvarCouponTypeId.IsNullable = false;
				colvarCouponTypeId.IsPrimaryKey = false;
				colvarCouponTypeId.IsForeignKey = false;
				colvarCouponTypeId.IsReadOnly = false;
				colvarCouponTypeId.DefaultSetting = @"";
				colvarCouponTypeId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCouponTypeId);
				
				TableSchema.TableColumn colvarIsSingleUse = new TableSchema.TableColumn(schema);
				colvarIsSingleUse.ColumnName = "IsSingleUse";
				colvarIsSingleUse.DataType = DbType.Boolean;
				colvarIsSingleUse.MaxLength = 0;
				colvarIsSingleUse.AutoIncrement = false;
				colvarIsSingleUse.IsNullable = false;
				colvarIsSingleUse.IsPrimaryKey = false;
				colvarIsSingleUse.IsForeignKey = false;
				colvarIsSingleUse.IsReadOnly = false;
				colvarIsSingleUse.DefaultSetting = @"";
				colvarIsSingleUse.ForeignKeyTableName = "";
				schema.Columns.Add(colvarIsSingleUse);
				
				TableSchema.TableColumn colvarUserId = new TableSchema.TableColumn(schema);
				colvarUserId.ColumnName = "UserId";
				colvarUserId.DataType = DbType.Guid;
				colvarUserId.MaxLength = 0;
				colvarUserId.AutoIncrement = false;
				colvarUserId.IsNullable = true;
				colvarUserId.IsPrimaryKey = false;
				colvarUserId.IsForeignKey = false;
				colvarUserId.IsReadOnly = false;
				colvarUserId.DefaultSetting = @"";
				colvarUserId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarUserId);
				
				TableSchema.TableColumn colvarNumberUses = new TableSchema.TableColumn(schema);
				colvarNumberUses.ColumnName = "NumberUses";
				colvarNumberUses.DataType = DbType.Int32;
				colvarNumberUses.MaxLength = 0;
				colvarNumberUses.AutoIncrement = false;
				colvarNumberUses.IsNullable = false;
				colvarNumberUses.IsPrimaryKey = false;
				colvarNumberUses.IsForeignKey = false;
				colvarNumberUses.IsReadOnly = false;
				
						colvarNumberUses.DefaultSetting = @"((0))";
				colvarNumberUses.ForeignKeyTableName = "";
				schema.Columns.Add(colvarNumberUses);
				
				TableSchema.TableColumn colvarExpirationDate = new TableSchema.TableColumn(schema);
				colvarExpirationDate.ColumnName = "ExpirationDate";
				colvarExpirationDate.DataType = DbType.DateTime;
				colvarExpirationDate.MaxLength = 0;
				colvarExpirationDate.AutoIncrement = false;
				colvarExpirationDate.IsNullable = true;
				colvarExpirationDate.IsPrimaryKey = false;
				colvarExpirationDate.IsForeignKey = false;
				colvarExpirationDate.IsReadOnly = false;
				colvarExpirationDate.DefaultSetting = @"";
				colvarExpirationDate.ForeignKeyTableName = "";
				schema.Columns.Add(colvarExpirationDate);
				
				TableSchema.TableColumn colvarXmlData = new TableSchema.TableColumn(schema);
				colvarXmlData.ColumnName = "XmlData";
				colvarXmlData.DataType = DbType.String;
				colvarXmlData.MaxLength = 1073741823;
				colvarXmlData.AutoIncrement = false;
				colvarXmlData.IsNullable = false;
				colvarXmlData.IsPrimaryKey = false;
				colvarXmlData.IsForeignKey = false;
				colvarXmlData.IsReadOnly = false;
				colvarXmlData.DefaultSetting = @"";
				colvarXmlData.ForeignKeyTableName = "";
				schema.Columns.Add(colvarXmlData);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["dashCommerce"].AddSchema("CSK_Coupons",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("CouponCode")]
		public string CouponCode 
		{
			get { return GetColumnValue<string>(Columns.CouponCode); }

			set { SetColumnValue(Columns.CouponCode, value); }

		}

		  
		[XmlAttribute("CouponTypeId")]
		public int CouponTypeId 
		{
			get { return GetColumnValue<int>(Columns.CouponTypeId); }

			set { SetColumnValue(Columns.CouponTypeId, value); }

		}

		  
		[XmlAttribute("IsSingleUse")]
		public bool IsSingleUse 
		{
			get { return GetColumnValue<bool>(Columns.IsSingleUse); }

			set { SetColumnValue(Columns.IsSingleUse, value); }

		}

		
		
		//private Guid? propUserId;
		[XmlAttribute("UserId")]
		public Guid UserId 
		{
			get
			{
			Guid? propUserId = GetColumnValue<Guid?>(Columns.UserId);
				if (!propUserId.HasValue)
					return Guid.Empty;
				return propUserId.Value;
			}

		   set { SetColumnValue(Columns.UserId, value); }

		}

		
		[XmlIgnore]
		public bool UserIdHasValue
		{
			get { return GetColumnValue<Guid?>("UserId") != null; }

			set
			{
				Guid? propUserId = GetColumnValue<Guid?>(Columns.UserId);
				if (!value)
					SetColumnValue(Columns.UserId, null);
				else if (value && !propUserId.HasValue)
					SetColumnValue(Columns.UserId, Guid.Empty);
		   }

		}

		
		  
		[XmlAttribute("NumberUses")]
		public int NumberUses 
		{
			get { return GetColumnValue<int>(Columns.NumberUses); }

			set { SetColumnValue(Columns.NumberUses, value); }

		}

		
		
		//private DateTime? propExpirationDate;
		[XmlAttribute("ExpirationDate")]
		public DateTime ExpirationDate 
		{
			get
			{
			DateTime? propExpirationDate = GetColumnValue<DateTime?>(Columns.ExpirationDate);
				if (!propExpirationDate.HasValue)
					return new DateTime(1900,01, 01);
				return propExpirationDate.Value;
			}

		   set { SetColumnValue(Columns.ExpirationDate, value); }

		}

		
		[XmlIgnore]
		public bool ExpirationDateHasValue
		{
			get { return GetColumnValue<DateTime?>("ExpirationDate") != null; }

			set
			{
				DateTime? propExpirationDate = GetColumnValue<DateTime?>(Columns.ExpirationDate);
				if (!value)
					SetColumnValue(Columns.ExpirationDate, null);
				else if (value && !propExpirationDate.HasValue)
					SetColumnValue(Columns.ExpirationDate, new DateTime(1900,01, 01));
		   }

		}

		
		  
		[XmlAttribute("XmlData")]
		public string XmlData 
		{
			get { return GetColumnValue<string>(Columns.XmlData); }

			set { SetColumnValue(Columns.XmlData, value); }

		}

		
		#endregion
		
		
			
		
		//no foreign key tables defined (0)
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varCouponCode,int varCouponTypeId,bool varIsSingleUse,Guid varUserId,int varNumberUses,DateTime varExpirationDate,string varXmlData)
		{
			Coupon item = new Coupon();
			
			item.CouponCode = varCouponCode;
			
			item.CouponTypeId = varCouponTypeId;
			
			item.IsSingleUse = varIsSingleUse;
			
			item.UserId = varUserId;
			
			item.NumberUses = varNumberUses;
			
			item.ExpirationDate = varExpirationDate;
			
			item.XmlData = varXmlData;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(string varCouponCode,int varCouponTypeId,bool varIsSingleUse,Guid varUserId,int varNumberUses,DateTime varExpirationDate,string varXmlData)
		{
			Coupon item = new Coupon();
			
				item.CouponCode = varCouponCode;
			
				item.CouponTypeId = varCouponTypeId;
			
				item.IsSingleUse = varIsSingleUse;
			
				item.UserId = varUserId;
			
				item.NumberUses = varNumberUses;
			
				item.ExpirationDate = varExpirationDate;
			
				item.XmlData = varXmlData;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn CouponCodeColumn
        {
            get { return Schema.Columns[0]; }

        }

        
        
        
        public static TableSchema.TableColumn CouponTypeIdColumn
        {
            get { return Schema.Columns[1]; }

        }

        
        
        
        public static TableSchema.TableColumn IsSingleUseColumn
        {
            get { return Schema.Columns[2]; }

        }

        
        
        
        public static TableSchema.TableColumn UserIdColumn
        {
            get { return Schema.Columns[3]; }

        }

        
        
        
        public static TableSchema.TableColumn NumberUsesColumn
        {
            get { return Schema.Columns[4]; }

        }

        
        
        
        public static TableSchema.TableColumn ExpirationDateColumn
        {
            get { return Schema.Columns[5]; }

        }

        
        
        
        public static TableSchema.TableColumn XmlDataColumn
        {
            get { return Schema.Columns[6]; }

        }

        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string CouponCode = @"CouponCode";
			 public static string CouponTypeId = @"CouponTypeId";
			 public static string IsSingleUse = @"IsSingleUse";
			 public static string UserId = @"UserId";
			 public static string NumberUses = @"NumberUses";
			 public static string ExpirationDate = @"ExpirationDate";
			 public static string XmlData = @"XmlData";
						
		}

		#endregion
		
		
		
	}

}

